/**
 * 页面对窗口的一些操作封装，用于渲染进程
 */

 const { ipcRenderer, remote } = require('electron');
//  const WinReg = require('winreg');
 const RUN_LOCATION = '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';
 const file = process.execPath;
 
 let flashTrayTimer = null;
 
 class WindowUtil{
     // 创建一个新的窗口
     static createWindow(url){
        const allWindow = remote.BrowserWindow.getAllWindows()
        const needWindow = allWindow.find(it => it.getURL() ===url)
        if(needWindow){
            this.flashFrame(needWindow)
            return
        }
        let win = new remote.BrowserWindow({ width: 800, height: 620,webPreferences: {
            nodeIntegration: true, // 可以在渲染层调用node的require
            // 在electron 10.0.0之后，remote模块默认关闭
            // 必须手动设置webPreferences中的enableRemoteModule为true之后才能使用
            enableRemoteModule: true, // 这里是关键设置
            contextIsolation: false,
          }, })
        win.webContents.openDevTools();
        win.on('close', function () { win = null })
        win.loadURL(url)
        win.show()
        return win
     }
     // 窗口最小化
     static minWindow(targetWindow =remote.getCurrentWindow() ) {
        targetWindow.minimize();
     }
     // 窗口最大化
     static maxWindow(isMaxed,targetWindow =remote.getCurrentWindow() ) {
         const browserWindow = targetWindow;
         if(!isMaxed) {
             browserWindow.unmaximize();
         } else {
             browserWindow.maximize();
         }
     }
     // 设置窗口是否能改变大小，参数true/false
     static setResizable(resizable,targetWindow =remote.getCurrentWindow()) {
        targetWindow.setResizable(resizable);
     }
     // 下载文件
     static download(url){
         remote.getCurrentWebContents().downloadURL(url);
     }
 
     // 隐藏窗口
     static hide(){
         const browserWindow = remote.getCurrentWindow();
         browserWindow.hide();
     }
 
     // 显示窗口
     static show(){
         const browserWindow = remote.getCurrentWindow();
         browserWindow.show();
     }
     // 窗口闪烁 其实就是指定某个窗口打开

     static flashFrame(targetWindow =remote.getCurrentWindow()){
         const browserWindow = targetWindow;
        
      //   if(browserWindow.isFocused() || browserWindow.isVisible())
         if(!browserWindow.isFocused()) {
             browserWindow.showInactive();
             browserWindow.flashFrame(true);
         }
     }
     // 设置窗口最前端显示
     static setAlwaysOnTop(top){
         const browserWindow = remote.getCurrentWindow();
         browserWindow.setAlwaysOnTop(top);
     }
 
     // 设置开机启动
     static enableAutoStart(callback) {
         let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION});
         key.set('EUC', WinReg.REG_SZ, file, (err)=> {
             console.log('设置自动启动'+err);
             callback(err);
         });
     }
     // 取消开机启动
     static disableAutoStart(callback) {
         let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION});
         key.remove('EUC',  (err)=>{
             console.log('取消自动启动'+err);
             callback(err);
         });
     }
     // 获取是否开机启动
     static getAutoStartValue(callback) {
         let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION});
         key.get('EUC', function (error, result) {
             console.log("查询自动启动:"+JSON.stringify(result));
             console.log("file:"+file);
             if (result) {
                 callback(true);
             }
             else {
                 callback(false);
             }
         });
     }
 
     /**
      * 托盘图标闪烁
      * @param flash true：闪烁；false：停止
      */
     static flashTray(flash) {
         let hasIcon = false;
         const tayIcon = './imgs/logo.ico';
         const tayIcon1 = './imgs/empty.png';
         if (flash) {
             if (flashTrayTimer) {
                 return;
             }
             flashTrayTimer = window.setInterval(() => {
                 ipcRenderer.send('ChangeTrayIcon', hasIcon ? tayIcon : tayIcon1);
                 hasIcon = !hasIcon;
             }, 500);
         } else {
             if(flashTrayTimer) {
                 window.clearInterval(flashTrayTimer);
                 flashTrayTimer = null;
             }
             ipcRenderer.send('ChangeTrayIcon', tayIcon);
         }
     }
 
 
 }

 export default WindowUtil